<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;

class Common extends Controller{
    public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function _initialize(){
        //判断系统是否开放
        $setting=setting();
        if($setting['sisclose']==1){
            die(json_encode(['zt'=>'-4','msg'=>$setting['sisclose_msg']]));
        }

   		//功能：API接收数据安全验证
		//验证失败 直接返回APP客户端
		//验证成功 返回整合头信息的数组
        $appdata=apijieshou();
        $header=$appdata['header'];	//头信息
        $userinfo=userinfobyuname(config('uname'));	//用户信息
        // die(json_encode($appdata));//打印接收数据



		//功能：验证合法登录 需要登陆的业务进行此验证
		//验证失败 直接返回APP客户端
		//验证成功 返回登陆相关信息数组
        if(!isset($header['sessionid'])){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }
        $islogin=apiislogin($userinfo,$header['pwd'],$header['sessionid']);

        //验证ajaxtoken是否重复提交
		//如果是 直接返回APP客户端错误信息 终止php业务
		//如果不是 返回缓存的tokenid
        $ajaxtoken_id=apiajaxtoken($header['ajaxtoken'],config('uname'));

        //业务通用常量 避免不必要的重复获取查询
        $this->appdata=$appdata;	//接收的所有数据 含header
        $this->userinfo=$userinfo;	//用户信息
        $this->setting=$setting;   //系统参数
        /*$this->appdata=$_POST;	//接收的所有数据 含header
        $this->userinfo=userinfobyuname($_POST['uname']);	//用户信息
        $this->setting=setting();*/
        if($this->userinfo['zt']==2){
            $this->check_operate();
        }
    }

    /**
     * @name 封号限制用户操作
     * @author zay
     * @Date 2020/4/6 0006 14:51
     */
    public function check_operate(){
        $allow_action  = [
            'setting','quotation','regain_user','gongdan_do','gongdan','gongdan_info','gongdan_info_do','login'
        ];
        $action = strtolower(request()->action());
        if(!in_array($action,$allow_action)){
            apifasong(['zt'=>'-3','msg'=>'封号不能操作']);
        }
    }

    public function mingdenggonghui(){  //明灯工会 指定团队进入验证
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        if($userinfo['uname']=='13189298186'){
            apifasong(['zt'=>'1']);
        }

        $position = chickusposition($userinfo,userinfobyuname('13189298186'));
        if($position && $position=='xia'){
            apifasong(['zt'=>'1']);
        }else{
            apifasong(['zt'=>'0','msg'=>'你所在的团队暂无权限']);
        }
    }

    public function renwujijin(){  //任务基金扣除5枚OME+5枚矿金开通权限
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        if(substr($userinfo['oright'],0,1)=='1'){
            apifasong(['zt'=>'1','msg'=>'你已开通','oright'=>$userinfo['oright']]);
        }
        if($userinfo['ome']<5) {
            apifasong(['zt'=>'0','msg'=>'OME余额不足，需要5枚OME+5枚矿金才可进入']);
        }
        if($userinfo['kuang']<5) {
            apifasong(['zt'=>'0','msg'=>'矿金余额不足，需要5枚OME+5枚矿金才可进入']);
        }

        Db::startTrans();   // 启动事务-------------
        $new_oright = '1'.substr($userinfo['oright'],1);
        $ret_user = db('user')->where('id',$userinfo['id'])->dec('ome',5)->dec('kuang',5)->update(['oright'=>$new_oright]);
        $ret_ome1 = ome1($userinfo,'-5');
        $log_ome = mylog('ome',$userinfo['uname'],'rwjj','',$userinfo['ome'],'-5','开通任务基金','','');  //记录
        $log_kuang = mylog('kuang',$userinfo['uname'],'rwjj','',$userinfo['kuang'],'-5','开通任务基金','','');  //记录
        if($ret_user && $ret_ome1 && $log_ome && $log_kuang){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'开通成功','oright'=>$new_oright]);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'开通失败']);
        }
    }
}